//
//  YCLTabBar.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/8.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

class YCLTabBar: UITabBar {

    //定义闭包属性
    var composeClosure:(() ->())?
    
    
    
    func composeBtnDidClick() {
        
        //modal出一个控制器，通过闭包实现
        composeClosure?()
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //设置按钮的frame值
        let buttonCount = YCLTabBarItemsCount
        // 宽度
        let width = self.frame.size.width / CGFloat(buttonCount)
        
        // 创建frame
        let newFrame = CGRect(x: 0, y: 0, width: width, height: frame.size.height)
        
        var index = 0
        for subView in subviews{
 
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                //frame可以平移
                subView.frame = CGRectOffset(newFrame, CGFloat(index) * width, 0)
                
                index += 1
                if index == 2 {
                    index += 1
                }
            }
        }
        
        compaseBtn.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.size.height)
    }
    
    //MARK: 懒加载
    lazy var compaseBtn:UIButton = {
    
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //监听按钮的点击
        btn.addTarget(self, action: Selector("composeBtnDidClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        //将按钮添加到父控件
         self.addSubview(btn)
        
        return btn
        
    
    }()
}
